package com.hyt.it.ogt.ks.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 刘恒活
 * @since 2020/9/16 9:42
 */
public class Base64Utils {
    public static void main(String[] args) {

    }

    public static final String PATTERN = "data:(image.+);base64,(.+)";
    public static byte[] string2Byte(String base64){
        Pattern p = Pattern.compile(PATTERN);
        Matcher m = p.matcher(base64);
        if(m.matches() && m.groupCount() >= 2){
            return org.springframework.util.Base64Utils.decodeFromString(m.group(2));
        }
        return org.springframework.util.Base64Utils.decodeFromString(base64);
    }

    public static String getExtension(String base64){
        Pattern p = Pattern.compile(PATTERN);
        Matcher m = p.matcher(base64);
        if(m.matches() && m.groupCount() >= 2){
            return m.group(1).split("/")[1];
        }
        return null;
    }
}
